package com.lc202407;

public class Solution917 {
    public String reverseOnlyLetters(String s) {
        char[] chars = s.toCharArray();
        int start = 0;
        int end = s.length() - 1;
        while (start < end) {
            char header = chars[start];
            char footer = chars[end];
            if (Character.isLetter(header) && Character.isLetter(footer)) {
                chars[start] = footer;
                chars[end] = header;
                start++;
                end--;
                continue;
            }
            if (!Character.isLetter(header)) {
                start++;
            }
            if (!Character.isLetter(footer)) {
                end--;
            }
        }
        return new String(chars);
    }
}
